package com.hu.bgp.server.domain;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import com.avaje.ebean.Query;
import com.hu.bgp.server.Persistence;

/**
 * De profile entity, voor logins en om scores op te zoeken en dergelijke.
 * 
 * @author ejvos
 */
@Entity
@Table(name = "bgp_profiles")
public class Profile
{
	@Id
	private int id;
	@Column
	private String name;
	@Column
	private String profileUuid;

	/**
	 * Always have an empty constructor for entities.
	 */
	public Profile()
	{
	}

	public void setId(int id)
	{
		this.id = id;
	}

	public int getId()
	{
		return id;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public String getName()
	{
		return name;
	}

	public void setProfileUuid(String profileUuid)
	{
		this.profileUuid = profileUuid;
	}

	public String getProfileUuid()
	{
		return profileUuid;
	}

	// DAO methods
	public List<Score> getScores(Persistence p)
	{
		Query<Score> q = p.getScoreServer().createQuery(Score.class);
		q.where().eq("profileId", id);
		return q.findList();
	}

	public List<Score> getScores(Persistence p, String gameTypeAlias)
	{
		Query<Score> q = p.getScoreServer().createQuery(Score.class);
		q.where().eq("profileId", id);
		q.where().eq("gameTypeAlias", gameTypeAlias);
		return q.findList();
	}

	public static Profile findForUuid(Persistence p, String profileUuid)
	{
		Query<Profile> q = p.getDefaultServer().createQuery(Profile.class);
		q.where().eq("profileUuid", profileUuid);
		return q.findUnique();
	}

	public static Profile findForId(Persistence p, Integer profileId)
	{
		return p.getDefaultServer().find(Profile.class, profileId.intValue());
	}

	public void save(Persistence p)
	{
		p.getDefaultServer().save(this);
	}
}